package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.ConditionDiscountModel;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 组合X元选Y件
 * @author yinhaijun
 * @date: 2020/5/19
 */
@Service("ComplexXyuanyProcessor")
public class ComplexXyuanyProcessor extends AbstractConditionDiscountProcessor {

    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;

    @Override
    public ConditionResult isCondition(ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(((DiscountBaseRequest)request).getThirdUserId(), ((DiscountBaseRequest)request).getAdminOperate().getChildId().toString(), conditionDiscountModel.getChannelCodes(), conditionDiscountModel.getPromotionMemberGradeList())){
            return new ConditionResult(false, "组合X元选Y件计算"+conditionDiscountModel.getPromotionCode()+"会员等级校验未通过");
        }
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(conditionDiscountModel.getPromotionId()));
        if(null == promotionRuleEntityList || promotionRuleEntityList.isEmpty()){
            return new ConditionResult(false, "没有规则数据");
        }
        // 总件数
        AtomicReference<Long> totalNum = new AtomicReference<>(0L);
        conditionDiscountModel.getThirdSkuIdList().forEach((thirdSkuId)->{
            GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
            totalNum.set(totalNum.get() + goodsDetailModel.getNum());
        });
        BigDecimal jicu = new BigDecimal(promotionRuleEntityList.get(0).getConditionValue());
        Integer y = promotionRuleEntityList.get(0).getContentValue().intValue();
        if(totalNum.get() < y ){//Y
            return new ConditionResult(false, "未达到条件");
        }
        ConditionResult conditionResult = new ConditionResult(true, "通过");
        // 总件数除Y值取倍数
        Long beisu = totalNum.get()/y;
        // 取余
        Long yusu = totalNum.get()%y;
        // （倍数+1）*基础值=应付价格
        BigDecimal yingfu = Money.multiply(new BigDecimal(beisu + 1), jicu);
        conditionResult.setObject(yingfu);
        return conditionResult;
    }

    @Override
    public void queryShoppingCartAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        querySettlementAfter(conditionResult, conditionDiscountModel, goodsDetailModelMap, request);
    }

    @Override
    public void querySettlementAfter(ConditionResult conditionResult, ConditionDiscountModel conditionDiscountModel, Map<String, GoodsDetailModel> goodsDetailModelMap, Object request){
        // 应付
        BigDecimal yingfu = (BigDecimal) conditionResult.getObject();
        // 总价-应付=优惠金额
        BigDecimal actualTotalAmount = conditionDiscountModel.getActualTotalAmount(goodsDetailModelMap);
        BigDecimal promotionDiscountTotalAmount = Money.subtract(actualTotalAmount, yingfu);
        conditionDiscountModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
    }
}

